home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / tsr.swg / 0011_Sample TSR Routine.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-08-27  |  2.3 KB  |  78 lines

  1. {
  2. CHRIS PRIEDE
  3.  
  4. > Can anyone give me any samples of TSR routines?
  5.  
  6.     My old example of a generic keyboard TSR has mysteriously
  7. disappeared, so I had to write a new one. This is as simple TSR as it
  8. can be: no stack switching, no DOS reentrancy check. In the form
  9. presented here it simply beeps when you press Shift-Esc. Set your own
  10. hotkey and rewrite TsrMain to turn it into something useful.
  11.  
  12.     Use Crt unit for screen writes and don't try to access files. Since
  13. it uses foreground program's stack, you shouldn't use deeply nested or
  14. recursive function calls, or declare large local variables. This is more
  15. on demo side, but will get you started.
  16. }
  17.  
  18. program GenericKeyboardTSR;
  19. {$M 0, 0, 512}      { reduce memory size }
  20. {$S-,R-}         { can't use stack or range checking in TSR }
  21.  
  22.  
  23. uses
  24.   Dos, Crt;
  25.  
  26. Const
  27.   RtShift       = $01;
  28.   LtShift       = $02;
  29.   AnyShift      = RtShift + LtShift;
  30.   Ctrl          = $04;
  31.   Alt           = $08;
  32.  
  33.   HotKey        = $01;      { Hotkey scan code, Esc }
  34.   HotShiftState = AnyShift; { Hotkey shift state }
  35.   FakeFlags     = 0;        { Fake flags for interrupt call }
  36.  
  37. type
  38.   IntProc = procedure(Flags : word);
  39.  
  40. var
  41.   OldInt09 : IntProc;
  42.   Popped   : boolean;
  43.  
  44.  
  45. procedure Enable; Inline($FB);      { inline macro -- STI }
  46.  
  47. procedure TsrMain;      { TSR main procedure, executed on hotkey }
  48. begin
  49.   Sound(400);           { Make noise (replace with something useful) }
  50.   Delay(100);
  51.   NoSound;
  52. end;
  53.  
  54. procedure NewInt09; interrupt;
  55. begin
  56.   Enable;               { Allow other interrupts }
  57.   if (not Popped) and (Port[$60] = HotKey) and  { if not in TSR already }
  58.      (Mem[$40 : $17] and $0F = HotShiftState) then { and hotkey detected }
  59.   begin
  60.     Popped := true;     { set Popped to avoid re-entry }
  61.     Port[$61] := Port[$61] or $80;      { reset keyboard }
  62.     Port[$61] := Port[$61] and not $80;
  63.     Port[$20] := $20;   { signal end of interrupt }
  64.     TsrMain;            { run TSR main procedure }
  65.     Popped := false;    { clear Popped and return }
  66.   end
  67.   else
  68.     OldInt09(FakeFlags); { call old handler }
  69. end;
  70.  
  71.  
  72. begin           { installation }
  73.   Popped := false;
  74.   GetIntVec($09, pointer(@OldInt09)); { Install int. handler}
  75.   SetIntVec($09, @NewInt09);
  76.   Keep(0);    { stay resident }
  77. end.
  78.